package com.example.charge

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.charge.databinding.ActivityChargeDetailBinding
import com.example.charge.entity.ChargeEntity
import com.example.common.view.view.BaseMvvmActivity

@Route(path = "/charge/ChargeDetailActivity")
class ChargeDetailActivity : BaseMvvmActivity<ActivityChargeDetailBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_charge_detail
    var chargeEntity : ChargeEntity? = null
    var chargeEntityAdd : ChargeEntity? = null
    private var actualMoney = 0

    override fun initView() {
        super.initView()

        chargeEntity = intent.getParcelableExtra("chargeEntity")
        chargeEntityAdd = intent.getParcelableExtra("chargeEntityAdd")
        if (chargeEntity != null){
            chargeEntity?.let { item ->
                actualMoney =item.receivableAmount
                mDataBinding.user.text = "户主: ${item.payerName}    联系电话: ${item.contactNumber}"
                mDataBinding.paymentTime.text = item.paymentTime
                mDataBinding.actualAmount.text = item.chargeAmount.toString()
                mDataBinding.chargeType.text = item.chargeType
                mDataBinding.effectiveDate.text = item.paymentTime
                mDataBinding.money.text ="￥${item.receivableAmount}.00"
            }
            mDataBinding.receivableAmount.text = "账单金额: ￥ ${actualMoney}.00"
        }
        if (chargeEntityAdd != null){
            chargeEntityAdd?.let { item ->
                actualMoney +=item.receivableAmount
                mDataBinding.user.text = "户主: ${item.payerName}    联系电话: ${item.contactNumber}"
                mDataBinding.paymentTimes.text = item.paymentTime
                mDataBinding.actualAmounts.text = item.chargeAmount.toString()
                mDataBinding.chargeTypes.text = item.chargeType
                mDataBinding.effectiveDates.text = item.paymentTime
                mDataBinding.moneys.text ="￥${item.receivableAmount}.00"
            }
            mDataBinding.receivableAmount.text = "账单金额: ￥ ${actualMoney}.00"
        }else{
            mDataBinding.ll.visibility = View.GONE
        }

        mDataBinding.cb.setOnClickListener {
            if (chargeEntityAdd == null){
                actualMoney = chargeEntity?.receivableAmount!!
                mDataBinding.actual.text =if(mDataBinding.cb.isChecked)" 已选金额: ￥${actualMoney}.00" else " 已选金额: ￥0.00"
                mDataBinding.cbAll.isChecked =  mDataBinding.cb.isChecked
            }else{
                AllCb()
            }
        }
        mDataBinding.cbs.setOnClickListener {
            AllCb()
        }


        mDataBinding.cbAll.setOnClickListener {
            if (mDataBinding.cbAll.isChecked){
                actualMoney = chargeEntity?.receivableAmount!! + chargeEntityAdd?.receivableAmount!!
                mDataBinding.actual.text = " 已选金额: ￥${actualMoney}.00"
                mDataBinding.cb.isChecked = true
                mDataBinding.cbs.isChecked = true
            } else {
                mDataBinding.actual.text = " 已选金额: ￥0.00"
                mDataBinding.cb.isChecked = false
                mDataBinding.cbs.isChecked = false
            }
        }
        //添加
        mDataBinding.add.setOnClickListener {
            ARouter.getInstance().build( "/charge/ChargeAddActivity")
                .withParcelable("chargeEntity",chargeEntity).navigation()
        }
        //付款
        mDataBinding.btnCollect.setOnClickListener {
            ARouter.getInstance().build("/charge/ChargeQRActivity")
                .withParcelable("chargeEntity",chargeEntity)
                .navigation()
        }
    }
    private fun AllCb(){
        if (mDataBinding.cb.isChecked && mDataBinding.cbs.isChecked){
            actualMoney = chargeEntity?.receivableAmount!! + chargeEntityAdd?.receivableAmount!!
            mDataBinding.actual.text = " 已选金额: ￥${actualMoney}.00"
            mDataBinding.cbAll.isChecked = true
        } else  if (mDataBinding.cb.isChecked || mDataBinding.cbs.isChecked){
            if (mDataBinding.cb.isChecked){
                actualMoney = chargeEntity?.receivableAmount!!
            } else {
                actualMoney = chargeEntityAdd?.receivableAmount!!
            }
            mDataBinding.actual.text = " 已选金额: ￥${actualMoney}.00"
            mDataBinding.cbAll.isChecked = false
        } else {
            actualMoney = 0
            mDataBinding.actual.text = " 已选金额: ￥0.00"
        }
    }
}